package com.epfresh.supplier.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.supplier.R;
import com.epfresh.api.widget.MsgView;
import com.epfresh.api.widget.tablayout.utils.UnreadMsgUtils;

/**
 * Created by wzh on 2016/5/6.
 */
public class HomeMenuAdapter extends BaseAdapter implements View.OnClickListener{

    public Context context;

    private int[] contents = new int[]{
            R.string.wait_money,
            R.string.wait_send,
            R.string.wait_receive,
            R.string.wait_take,
            R.string.wait_service,
            R.string.all_order

    };

    private int[] images = new int[]{
            R.mipmap.obligations,
            R.mipmap.overhang,
            R.mipmap.take,
            R.mipmap.service,
            R.mipmap.home_evaluate,
            R.mipmap.orders

    };

    private int[] msgs = new int[]{
            0, 0, 0, 0, 0, 0
    };

    private AdapterView.OnItemClickListener onItemClickListener;

    public HomeMenuAdapter(Context context) {
        this.context = context;
    }

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getCount() {
        return 7;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HomeMenuViewHolder vh = null;
        if (convertView == null) {
            vh = new HomeMenuViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_menu_item, null, false);
            vh.ivTop = (ImageView) convertView.findViewById(R.id.iv_top);
            vh.tvContent = (TextView) convertView.findViewById(R.id.tv_obligations);
            vh.msgView = (MsgView) convertView.findViewById(R.id.rtv_msg_tip);
            vh.vRightLine = convertView.findViewById(R.id.v_right_line);
            vh.vRightLineBottom = convertView.findViewById(R.id.v_right_line_bottom);
            vh.vRightLineTop = convertView.findViewById(R.id.v_right_line_top);
            vh.vBottomLine = convertView.findViewById(R.id.v_bottom_line);
            vh.vBottomLineLeft = convertView.findViewById(R.id.v_bottom_line_left);
            vh.vBottomLineRight = convertView.findViewById(R.id.v_bottom_line_right);
            convertView.setTag(vh);
        } else {
            vh = (HomeMenuViewHolder) convertView.getTag();
        }

        vh.ivTop.setImageResource(images[position]);
        vh.tvContent.setText(contents[position]);
        if (msgs[position] > 0) {
            vh.msgView.setVisibility(View.VISIBLE);
            UnreadMsgUtils.show(vh.msgView, msgs[position]);
        } else {
            vh.msgView.setVisibility(View.GONE);
        }


        if ((position + 1) % 3 == 0) {
            vh.vRightLineTop.setVisibility(View.GONE);
            vh.vRightLineBottom.setVisibility(View.GONE);
            vh.vRightLine.setVisibility(View.GONE);
        } else {
            vh.vRightLine.setVisibility(View.VISIBLE);
            if (position / 3 == 0) {
                vh.vRightLineBottom.setVisibility(View.VISIBLE);
                vh.vRightLineTop.setVisibility(View.GONE);
            } else {
                vh.vRightLineTop.setVisibility(View.VISIBLE);
                vh.vRightLineBottom.setVisibility(View.GONE);
            }

        }


        if (position / 3 == 1) {
            vh.vBottomLine.setVisibility(View.INVISIBLE);
            vh.vBottomLineRight.setVisibility(View.GONE);
            vh.vBottomLineLeft.setVisibility(View.GONE);
        } else {
            if ((position + 1) % 3 == 1) {
                vh.vBottomLineRight.setVisibility(View.VISIBLE);
                vh.vBottomLineLeft.setVisibility(View.GONE);
            } else if ((position + 1) % 3 == 0) {
                vh.vBottomLineRight.setVisibility(View.GONE);
                vh.vBottomLineLeft.setVisibility(View.VISIBLE);
            }
            vh.vBottomLine.setVisibility(View.VISIBLE);
        }

        convertView.setTag(R.id.item_key_position, position);
        convertView.setOnClickListener(this);

        return convertView;
    }

    public void showMsg(int position, int num) {
        if (position < msgs.length) {
            msgs[position] = num;
            notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_position);
        if (o != null && o instanceof Integer){
            if (onItemClickListener != null) {
                int position = (int) o;
                onItemClickListener.onItemClick(null, v, position, position);
            }
        }
    }

    public class HomeMenuViewHolder {
        ImageView ivTop;
        TextView tvContent;
        MsgView msgView;
        View vRightLine;
        View vRightLineTop;
        View vRightLineBottom;
        View vBottomLine;
        View vBottomLineRight;
        View vBottomLineLeft;
    }
}
